'use strict';

/**
 * @ngdoc service
 * @name jgappApp.FormSubmit
 * @description
 * # FormSubmit
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('FormSubmit', ['$http', 'openApi', 'Util', function($http, openApi, Util) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var FormSubmit = function(options) {
            this.options = options;
            this.url=options.url||openApi.start
            this.submit();
        };
        FormSubmit.prototype.submit = function() {
            $http.post(this.url + this.options.urlParam, this.options.param).success(function(data, status, headers, config) {
                if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    Util.clearFormData();
                    Util.Toast(data.result, {
                        callback: Util.goBack,
                        time:1000
                    });
                } else {
                    Util.Toast(data.result);
                }
            }.bind(this)).error(function(data, status, headers, config) {
                // Util.Toast("no data!");

            }.bind(this));
        };
        return FormSubmit;
    }]);
